<?php
class MovieModel extends BaseModel
{
    public function __construct()
    {
        parent::__construct();
        $this->movie_db = new MovieDb();
    }

    public function addUri($data)
    {
        $movie_uri_db = new MovieUriDb();
        $data['pub_time'] = time();
        $movie_uri_db->insertRow($data);
        if(($data['is_free'] == 1) && ($data['is_direct_download'] == 1)) {
            $where = array(
                'id' => $data['movie_id'],
            );
            $data = array(
                'is_free_direct_download' => 1,
            );
            $this->movie_db->update($data, $where);
        }
        return true;
    }

    public function get($id)
    {
        $where = array(
            'id' => $id,
        );
        $r = $this->movie_db->selectRow($where);
        if(!empty($r)) {
            return $r;
        }
        $app_conf = $this->conf->get('douban', 'app');
        $format_conf = $this->conf->get('douban', 'format');
        $api = new Douban_Apiv2($app_conf, array(), $format_conf);
        $r = $api->getMovie($id);
        $data = $r;
        $data['id'] = $id;
        $data['poster_uri'] = str_replace('spic', 'lpic', $r['poster_uri']); //豆瓣api给的图太小了……spic、mpic、lpic……豆瓣网站用的是mpic，我也用mpic，即可使用浏览器缓存，无请求。mpic 还是看不清，于是用lpic
        $data['year'] = $r['attrs']['year'][0];
        $data['update_time'] = time();
        $data['rating_percent'] = $r['rating']['average'] / $r['rating']['max'];
        $data['other_names'] = json_encode(array($r['alt_title']));
        $this->movie_db->insertRow($data);
        return $data;
    }

    public function getLastestMovies($limit_start, $limit_length)
    {
        $movie_db = new MovieDb();
        $where = array(
            'is_free_direct_download' => 1,
        );
        return $movie_db->selectRows($where, '*', $limit_start, $limit_length, '', 'update_time desc');
    }

    public function getProviders()
    {
        $provider_db = new ProviderDb();
        return $provider_db->selectRows();
    }

    public function getUri($id, $passkey='')
    {
        $movie_uri_db = new MovieUriDb();
        $where = array(
            'movie_id' => $id,
        );
        $order_by = 'provider_id ASC';
        $r = $movie_uri_db->selectRows($where, '*', '', '', '', $order_by);
        $uri_lib = new UriLib();
        //由于无法识别是bt还是pt，所以在movie_uri表中加入protocol字段
        //由于添加uri时只知道provider_id，不知道protocol，所以把protocol放到provider表中。
        $providers = $this->getProviders();
        $map = array();
        foreach($providers as $one) {
            $map[$one['id']] = $one['protocol'];
        }
        $data = array();
        foreach($r as $one) {
            if(in_array($map[$one['provider_id']], array(ProviderDb::$protocol['bt'], ProviderDb::$protocol['pt']))) {
                $torrent_db = new TorrentDb();
                $where = array(
                    'id' => $one['torrent_id'],
                );
                $one['torrent'] = $torrent_db->selectRow($where);
            }
            switch($map[$one['provider_id']]) {
                case ProviderDb::$protocol['bt'] :
                    $one['uri'] = $uri_lib->getBt($one['torrent_id']);
                    break;
                case ProviderDb::$protocol['pt'] :
                    $one['uri'] = $uri_lib->getPt($one['torrent_id']);
                    $one['bt_uri'] = $uri_lib->getBt($one['torrent_id']);
                    break;
            }
            $data[] = $one;
        }
        return $data;
    }

    public function update($data)
    {
        $where = array(
            'id' => $data['id'],
        );
        $this->movie_db->update($data, $where);
        return $id;
    }
}
?>
